package com.leonzx.base.service.file;

import com.leonzx.base.config.coss.OSSClient;
import com.leonzx.base.config.coss.OSSClientProperty;
import com.leonzx.base.config.coss.client.FileBytesResponse;
import com.leonzx.base.config.coss.common.OSSClientMessage;
import com.leonzx.base.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/7/12 16:27
 */
public class CustomOSSFileUploadService extends BaseService implements FileUploadService {
    private OSSClient ossClient;

    public CustomOSSFileUploadService() {
        OSSClientProperty ossClientProperty = new OSSClientProperty();
        ossClientProperty.setRemote("http://172.16.152.222:18000/");
        ossClientProperty.setAppid("ossgm73q5");
        ossClientProperty.setAppsecret("u296a33u");
        ossClientProperty.setProject("leonbase");
        this.ossClient = new OSSClient(ossClientProperty);
    }

    @Override
    public String upload(InputStream inputStream, String key) {
        try {
            File file = new File(Class.class.getClass().getResource("/").getPath() + key);
            inputstreamtofile(inputStream, file);
            OSSClientMessage<FileBytesResponse> ossClientMessage = ossClient.uploadFileByForm(file);
            if (Objects.equals(ossClientMessage.getCode(), "8200")) {
                FileBytesResponse fileBytesResponse = ossClientMessage.getData();
                file.delete();
                return fileBytesResponse.getId();
            }
            file.delete();
        } catch (Exception e) {
            logger.error("上传失败", e);
        }
        return "";
    }

    @Override
    public String upload(MultipartFile file, String key) {
        key = "";
        try {
            key = upload(file.getInputStream(), key);
        } catch (IOException e) {
            logger.error("上传失败");
        }
        return key;
    }

    @Override
    public InputStream download(String key) {
        String url = getUrlByKey(key);
        if (StringUtils.isNotBlank(key)) {
            InputStream is = url2Inputstream(url);
            return is;
        }
        return null;
    }

    @Override
    public boolean delete(String key) {
        OSSClientMessage<FileBytesResponse> ossClientMessage = ossClient.delete(key);
        if (Objects.equals(ossClientMessage.getCode(), "8200")) {
            return true;
        }
        return false;
    }

    @Override
    public String getUrlByKey(String key) {
        OSSClientMessage<FileBytesResponse> ossClientMessage = ossClient.downloadUrl(key);
        if (Objects.equals(ossClientMessage.getCode(), "8200")) {
            FileBytesResponse fileBytesResponse = ossClientMessage.getData();
            return fileBytesResponse.getUrl();
        }
        return "";
    }

    private void inputstreamtofile(InputStream ins, File file) throws IOException {
        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }

    private InputStream url2Inputstream(String path) {
        URL url = null;
        InputStream is = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();//利用HttpURLConnection对象,我们可以从网络中获取网页数据.
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();    //得到网络返回的输入流

        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }
}
